package com.sne.airways.system.service.impl;

import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.basecore.service.impl.BaseServiceImpl;
import com.sne.airways.basecore.utils.PasswordHelper;
import com.sne.airways.system.annotation.MethodLog;
import com.sne.airways.system.dao.UserDAO;
import com.sne.airways.system.dto.TdUserCheckDTO;
import com.sne.airways.system.dto.TdUserDTO;
import com.sne.airways.system.enums.ModuleEnum;
import com.sne.airways.system.enums.OpTypeEnum;
import com.sne.airways.system.pojo.TdSubsection;
import com.sne.airways.system.pojo.TdUser;
import com.sne.airways.system.pojo.TdUserRole;
import com.sne.airways.system.service.UserService;
import com.sne.airways.system.utils.AppUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: skyline{http://my.oschina.net/skyline520}
 * Date: 12-11-23
 * Time: 下午5:49
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends BaseServiceImpl implements UserService {
    @Autowired
    private UserDAO userDAO;
    /**
     * 查询用户列表
     * @param userNo
     * @param userName
     * @param status
     * @param start
     * @param limit
     * @return
     */
    public List<TdUserDTO> queryUserList(String userNo, String userName, String status, int start, int limit) {
        List<TdUser> userList = userDAO.queryUserList(userNo, AppUtils.getCurrentUser().getUserNo(), userName, status, start, limit);
        List<TdUserDTO> finalList = new ArrayList<TdUserDTO>();
        for(TdUser user:userList){
            TdUserDTO   dto   = new TdUserDTO();
            String[] ignoreProperties = {"createUser","modifyUser","roles","subsectionId","subsectionName"};
            BeanUtils.copyProperties(user, dto, ignoreProperties);
            //设置部门Id
            dto.setSubsectionId(user.getSubsectionId() ==null?null:user.getSubsectionId().getSubsectionId());
            //设置部门名称
            dto.setSubsectionName(user.getSubsectionId() == null ? null : user.getSubsectionId().getSubsectionName());
            //设置创建人
            dto.setCreateUser(user.getCreateUser() ==null?null:user.getCreateUser().getUserName());
            //设置修改人
            dto.setModifyUser(user.getModifyUser() == null ? null : user.getModifyUser().getUserName());
            finalList.add(dto);
        }
        return finalList;
    }

    /**
     * 查询用户记录总数
     * @param userNo
     * @param userName
     * @param status
     * @return
     */
    public Integer queryUserCount(String userNo, String userName, String status) {
        Integer count = userDAO.queryUserCount(userNo,AppUtils.getCurrentUser().getUserNo(),userName,status);
        return count;
    }
    /**
     * 保存用户
     * @param userNo
     * @param userName
     * @param password
     * @param subsectionId
     * @param userPhone
     * @param userTel
     * @param userMobile
     * @param userQQ
     * @param userMsn
     * @param userMail
     * @param userRemark
     * @param status
     * @param version
     * @return
     * @throws
     *
     */
    @MethodLog(opType = OpTypeEnum.添加用户,module = ModuleEnum.用户管理)
    public void saveUser(String userNo, String userName, String password, String subsectionId,
                              String userPhone, String userTel, String userMobile, String userQQ, String userMsn,
                              String userMail, String userRemark, String status, int version) throws AppException {
        //首先查询用户名是否重复
        int count = userDAO.queryUserCount(null,userNo,null);
        if(count > 0){
            throw new AppException("用户已经存在");
        }
        String encodePassword = PasswordHelper.encodePassword(password);
        TdSubsection subsection = null;
        if(StringUtils.isNotBlank(subsectionId)){
            //如果subsectionId 不为空 去查询分部对象
            subsection = loadObject(TdSubsection.class,subsectionId);
        }
        TdUser user = new TdUser();
        user.setUserNo(userNo);
        user.setUserName(userName);
        user.setPassword(encodePassword);
        user.setSubsectionId(subsection);
        user.setUserPhone(userPhone);
        user.setUserTel(userTel);
        user.setUserMobile(userMobile);
        user.setUserQQ(userQQ);
        user.setUserMsn(userMsn);
        user.setUserMail(userMail);
        user.setUserRemark(userRemark);
        user.setVersion(version);
        user.setStatus(status);
        user.setCreateDate(new Date());
        user.setCreateUser(AppUtils.getCurrentUser());
        saveObject(user);
        TdUserDTO dto = new TdUserDTO();
        String[] ignoreProperties = {"subsectionId","roles","createUser","modifyUser"};
        BeanUtils.copyProperties(user, dto, ignoreProperties);
        //设置创建人
        dto.setCreateUser(user.getCreateUser() == null ? null : user.getCreateUser().getUserName());
        //设置修改人
        dto.setModifyUser(user.getModifyUser() == null ? null : user.getModifyUser().getUserName());
        //设置部门ID
        dto.setSubsectionId(user.getSubsectionId() == null ? null : user.getSubsectionId().getSubsectionId());
    }

    /**
     * 编辑用户
     *
     * @param userId
     * @param userNo
     * @param userName
     * @param password
     * @param subsectionId
     * @param userPhone
     * @param userTel
     * @param userMobile
     * @param userQQ
     * @param userMsn
     * @param userMail
     * @param userRemark
     * @param status
     * @param version
     * @return
     * @throws AppException
     *
     */
    @MethodLog(opType = OpTypeEnum.编辑用户,module = ModuleEnum.用户管理)
    public void updateUser(String userId, String userNo, String userName, String password,
                                String subsectionId, String userPhone, String userTel, String userMobile,
                                String userQQ, String userMsn, String userMail, String userRemark, String status, int version) throws AppException {
        TdSubsection subsection = null;
        if(StringUtils.isNotBlank(subsectionId)){
            //如果subsectionId 不为空 去查询分部对象
            subsection = loadObject(TdSubsection.class,subsectionId);
        }
        TdUser user = loadObject(TdUser.class,userId);
        isCurrentVersion(user,version);
        user.setUserNo(userNo);
        user.setUserName(userName);
        user.setSubsectionId(subsection);
        user.setUserPhone(userPhone);
        user.setUserTel(userTel);
        user.setUserMobile(userMobile);
        user.setUserQQ(userQQ);
        user.setUserMsn(userMsn);
        user.setUserMail(userMail);
        user.setUserRemark(userRemark);
        user.setVersion(version);
        user.setStatus(status);
        user.setModifyDate(new Date());
        user.setModifyUser(AppUtils.getCurrentUser());
        updateObject(user);
    }

    /**
     * 根据userId和version 删除用户对象
     *
     * @param userId
     * @param version
     * @throws
     *
     */
    @MethodLog(opType = OpTypeEnum.删除用户,module = ModuleEnum.用户管理)
    public void deleteUser(String userId, int version) throws AppException {
        TdUser user = loadObject(TdUser.class,userId);
        user.setVersion(version);
        deleteObject(user);
    }

    @Override
    public List<TdUserCheckDTO> queryUserCheckedList(String roleId, String userNo, String userName, String status) throws AppException {
        List<TdUser> userList = userDAO.queryUserCheckedList(userNo,userName,status);
        List<TdUserCheckDTO> finalList = new ArrayList<TdUserCheckDTO>();
        for(TdUser user:userList){
            TdUserCheckDTO   dto   = new TdUserCheckDTO();
            String[] ignoreProperties = {"createUser","modifyUser","roles","subsectionId","subsectionName"};
            BeanUtils.copyProperties(user, dto, ignoreProperties);
            Set<TdUserRole> roleSet = user.getRoles();
            boolean flag = false;
            if(StringUtils.isNotBlank(roleId)){
                for(TdUserRole role:roleSet){
                    if(role.getRole().getRoleId().equals(roleId)){
                        flag = true;
                        break;
                    }
                }
            }
            dto.setChecked(flag);
            //设置部门Id
            dto.setSubsectionId(user.getSubsectionId() ==null?null:user.getSubsectionId().getSubsectionId());
//            //设置部门名称
            dto.setSubsectionName(user.getSubsectionId() == null ? null : user.getSubsectionId().getSubsectionName());
            //设置创建人
            dto.setCreateUser(user.getCreateUser() ==null?null:user.getCreateUser().getUserName());
            //设置修改人
            dto.setModifyUser(user.getModifyUser() == null ? null : user.getModifyUser().getUserName());
            finalList.add(dto);
        }
        return finalList;
    }

    /**
     * 根据roleId 以及ids 保存用户角色关联关系
     * @param userId
     * @param ids
     * @param version
     */
    @Override
    public void saveUserRole(String userId, String[] ids, int version) {
    }
}
